local shenshi = fk.CreateSkill{
  name = "mod__shenshi",
}
local U = require "packages/utility/utility"
local shenshi_ = {
  anim_type = "switch",
  switch_skill_name = "mod__shenshi",
  card_num = 1,
  target_num = 1,
  prompt = "#mod__shenshi",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude() and
      player:getSwitchSkillState(self.name, false) == fk.SwitchYang
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and to_select ~= Self.id then
      local n = 0
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p ~= Self and p:getHandcardNum() > n then
          n = p:getHandcardNum()
        end
      end
      return Fk:currentRoom():getPlayerById(to_select):getHandcardNum() == n
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:obtainCard(target.id, effect.cards[1], true, fk.ReasonGive)
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
    if player:getMark("shenshi_dying-phase") > 0 and not player.dead and table.find(room.alive_players, function(p) return p:getHandcardNum() < 4 end) then
      local targets = table.map(table.filter(room.alive_players, function(p) return p:getHandcardNum() < 4 end), Util.IdMapper)
      local to = room:askForChoosePlayers(player, targets, 1, 1, "#mod__shenshi-choose", self.name, true)
      if #to > 0 then
        local p = room:getPlayerById(to[1])
        p:drawCards(4 - p:getHandcardNum(), self.name)
      end
      if player:getMark("shenshi_dying-phase") > 0 then
        room:setPlayerMark(player, "shenshi_dying-phase", 0)
      end
    end
  end,
}
local shenshiYin = {
  switch_skill_name = "mod__shenshi",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("mod__shenshi") and player:getSwitchSkillState("mod__shenshi", false) == fk.SwitchYin and
      data.from and data.from ~= player and not data.from.dead
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, "mod__shenshi", nil, "#mod__shenshi-invoke::"..data.from.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("mod__shenshi")
    room:notifySkillInvoked(player, "mod__shenshi", "switch")
    local from = data.from
    room:doIndicate(player.id, {from.id})
    if not from:isKongcheng() then
      U.viewCards (player, from:getCardIds(Player.Hand), "mod__shenshi")
    end
    if player:isNude() then return end
    local card = room:askForCard(player, 1, 1, true, "mod__shenshi", false, ".", "#mod__shenshi-give::"..data.from.id)
    room:obtainCard(from.id, card[1], false, fk.ReasonGive)
    local mark = player:getTableMark("mod__shenshi-turn")
    table.insert(mark, {from.id, card[1]})
    room:setPlayerMark(player, "mod__shenshi-turn", mark)
  end,
}
local shenshi_trigger = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Finish and player:getMark("mod__shenshi-turn") ~= 0 and player:getHandcardNum() < 4 then
      for _, t in ipairs(player:getMark("mod__shenshi-turn")) do
        local p = player.room:getPlayerById(t[1])
        if p and table.contains(p:getCardIds("he"), t[2]) then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("mod__shenshi")
    room:notifySkillInvoked(player, "mod__shenshi", "drawcard")
    if not table.find(room.alive_players, function(p) return p:getHandcardNum() < 4 end) then return end
    local targets = table.map(table.filter(room.alive_players, function(p) return p:getHandcardNum() < 4 end), Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#mod__shenshi-choose", self.name, true)
    if #to > 0 then
      local p = room:getPlayerById(to[1])
      p:drawCards(4 - p:getHandcardNum(), self.name)
    end
  end,
}
local shenshi_detect = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return data.damage and data.damage.skillName == "mod__shenshi" and data.damage.from and data.damage.from == player and not player.dead
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "shenshi_dying-phase", 1)
  end
}
shenshi:addEffect("active", shenshi_)
shenshi:addEffect(fk.Damaged, shenshiYin)
shenshi:addEffect(fk.EventPhaseStart, shenshi_trigger)
shenshi:addEffect(fk.EnterDying, shenshi_detect)
return shenshi